package com.springbootmybatiscurd.controller;

import com.springbootmybatiscurd.pojo.Emp;
import com.springbootmybatiscurd.pojo.PageBean;
import com.springbootmybatiscurd.pojo.Query;
import com.springbootmybatiscurd.pojo.Result;

import com.springbootmybatiscurd.service.empService;
import com.springbootmybatiscurd.utils.UploadFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@Slf4j
@RestController
@CrossOrigin(origins = "*")   //设置跨域
@RequestMapping("/emps")
public class controllerEmp {
@Autowired
private empService empService;
    /**
     * 查询所有员工信息
     * */

    @GetMapping
    public Result getEmpData(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10") Integer pageSize)
      {
       PageBean pageBean = empService.listEmpService(page, pageSize);
         return  Result.success(pageBean);
    }


    /**
     * 添加员工
     * */

    @PostMapping
      public Result addEmp(@RequestBody Emp emp) {

        String message = empService.addEmpService(emp);

        return Result.success(message);
    }


    /**
     * 查询员工
     * */

    @GetMapping("/queryEmp")
        public Result queryEmp(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                               String name, short gender, @DateTimeFormat(pattern = "yy-MM-dd") String startTime,@DateTimeFormat(pattern = "yy-MM-dd") String endTime) {
        log.info("{},{},{},{},{},{}",page, pageSize,name,gender,startTime,endTime);
        LocalDate newStartTime = null;
        LocalDate newEndTime = null;
        if(!startTime.equals("")|| !endTime.equals("")){
            newStartTime = LocalDate.parse(startTime);
             newEndTime= LocalDate.parse(endTime);
        }


        PageBean pageBean = empService.pageService(page, pageSize,name,gender,newStartTime,newEndTime);
         return  Result.success(pageBean);

    }


     /**
      * 批量删除员工和单个删除员工
      * */

     @DeleteMapping("/{ids}")
     public Result deleteAll(@PathVariable List<Integer> ids)  {
         log.info("{}",ids);
         List<Integer> integers = empService.deleteAllService(ids);
         return  Result.success(integers.size());
     }

     /**
      * 查询回显
      * */
     @GetMapping("/{id}")
     public Result getById(@PathVariable Integer id){
         log.info("",id);
         Emp byIdService = empService.getByIdService(id);

         return Result.success(byIdService);
     }


    /**
     * 修改员工
     * */

    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {

        Emp updatedEmp= empService.updateEmpService(emp);
        log.info("{},{},{},{},{},{}",emp);

        return Result.success(updatedEmp);
    }

}
